package com.study.group.modules.mongo.dao.impl;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.study.group.common.entrty.Person;
import com.study.group.modules.mongo.dao.PersonDao;
import com.study.group.modules.mongo.service.PersonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description Dao
 * @Author      yanghanwei
 * @Mail        yanghanwei@geotmt.com
 * @Date        2019/4/26 12:02
 * @Version     v1
 **/
@Repository
public class PersonDaoImpl implements PersonDao {

    private static final Logger logger = LoggerFactory.getLogger(PersonDaoImpl.class);

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public List<Person> list() {
        logger.info("--------------------->[MongoDB dao list start]");
        return  mongoTemplate.findAll(Person.class);
    }

    @Override
    public Person save(Person person) {
        logger.info("--------------------->[MongoDB save start]");
        return mongoTemplate.save(person);
    }

    @Override
    public Person getById(String id) {
        logger.info("--------------------->[MongoDB getById start]");
        return mongoTemplate.findById(id, Person.class);
    }

    @Override
    public List<Person> getByName(String name) {
        logger.info("--------------------->[MongoDB getByName start]");
        Query query = new Query(Criteria.where("name").is(name));
        return mongoTemplate.find(query, Person.class, name);
    }

    @Override
    public Integer update(Person person) {
        logger.info("--------------------->[MongoDB update start]");
        String id = person.getId();
        Query query = new Query(Criteria.where("id").is(id));
        Update update = new Update().set("age",person.getAge()).set("人妖",person.getSex());
        Person byId = mongoTemplate.findById(id, Person.class);
        // updateFirst 更新查询返回结果集的第一条
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Person.class);
        // updateMulti 更新查询返回结果集的全部
//        UpdateResult updateResult1 = mongoTemplate.updateMulti(query, update, Person.class);
        // upsert 更新对象不存在则去添加
//        UpdateResult upsert = mongoTemplate.upsert(query, update, Person.class);
        return 1;
    }

    @Override
    public Integer delete(Person person) {
        logger.info("--------------------->[MongoDB delete start]");
        mongoTemplate.remove(person);
        return null;
    }

    @Override
    public Integer deleteById(String id) {
        logger.info("--------------------->[MongoDB deleteById start]");
        Person byId = mongoTemplate.findById(id, Person.class);
        if(null != byId){
            DeleteResult remove = mongoTemplate.remove(byId);
            System.out.println(remove);
        }
        return null;
    }
}
